package com.kpx.app.adapter;

import android.content.Context;
import android.view.View;


import com.kpx.app.Application;
import com.kpx.app.R;
import com.kpx.app.activity.ActivityResetPwd;
import com.kpx.app.adapter.baseadapter.BaseAdapter;
import com.kpx.app.adapter.baseadapter.ViewHolder;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.GirlItemData;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.ImageLoader;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.wigdet.ScaleImageView;

import java.util.List;


/**
 * Author: Othershe
 * Time: 2016/8/18 21:59
 */
public class GirlItemAdapter extends BaseAdapter<GirlItemData> {

    public GirlItemAdapter(Context context, List<GirlItemData> datas, boolean isOpenLoadMore) {
        super(context, datas, isOpenLoadMore);
    }

    @Override
    protected void convert(ViewHolder holder, final GirlItemData girlItemData) {
        ScaleImageView imageView = holder.getView(R.id.girl_item_iv);
        imageView.setInitSize(girlItemData.getWidth(), girlItemData.getHeight());
        ImageLoader.load(Application.mContext,
                girlItemData.getUrl(), imageView);
        holder.setBgRes(R.id.img_isshow,girlItemData.isshow()==1 ?R.drawable.certification_photo_show:R.drawable.certification_photo_show2);
        holder.getView(R.id.img_isshow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSetShow(girlItemData);
            }
        });
    }

    private void toSetShow(final GirlItemData girlItemData) {
        ((ActivityBase)mContext).showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("dmId",girlItemData.getId());
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                ((ActivityBase)mContext).dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        mDatas.get(i).setIsshow(2);
                    }
                    girlItemData.setIsshow(1);
                    notifyDataSetChanged();
                } else {
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ((ActivityBase)mContext).dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(mContext, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_PHOTO_COVER, true, map);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_girl_layout;
    }
}
